home *** CD-ROM | disk | FTP | other *** search
/ Clickx 47 / Clickx 47.iso / assets / software / switchproxy.xpi / chrome / switchproxy.jar / content / datasource.js < prev    next >
Encoding:
JavaScript  |  2006-04-11  |  10.5 KB  |  387 lines

  1. //DataSource
  2. var gSProxyDs            = null;
  3. var gSProxyRdf            = null;
  4. var gSProxyRdfC            = null;
  5. var gSProxyRDFUtil        = null
  6.  
  7. //Initializes the RDF Datasource components
  8. function switchProxy_ds_initDataSource(){
  9.     try{
  10.         
  11.         // Datasource URI
  12.         if(gSProxyRdfDataSouce == "rdf:local-store"){
  13.             var file = Components.classes["@mozilla.org/file/directory_service;1"].getService(Components.interfaces.nsIProperties);
  14.             var io = Components.classes["@mozilla.org/network/io-service;1"].getService(Components.interfaces.nsIIOService);
  15.             file = file.get("PrefD", Components.interfaces.nsILocalFile);
  16.             file.appendRelativePath("localstore.rdf");
  17.             
  18.             io = io.newFileURI(file);
  19.             gSProxyRdfDataSouce = io.spec;
  20.         }
  21.     
  22.         if(gSProxyRdf == null)
  23.             gSProxyRdf = Components.classes["@mozilla.org/rdf/rdf-service;1"].getService(Components.interfaces.nsIRDFService);
  24.         if(gSProxyDs == null)
  25.             gSProxyDs    = gSProxyRdf.GetDataSourceBlocking(gSProxyRdfDataSouce);
  26.         if(gSProxyRDFUtil == null)
  27.             gSProxyRDFUtil = Components.classes["@mozilla.org/rdf/container-utils;1"].getService(Components.interfaces.nsIRDFContainerUtils);
  28.         if(gSProxyRdfC == null){
  29.             gSProxyRdfC = Components.classes["@mozilla.org/rdf/container;1"].createInstance(Components.interfaces.nsIRDFContainer);
  30.             
  31.             //Get or Add Sequence
  32.             try{
  33.                 gSProxyRdfC.Init(gSProxyDs, gSProxyRdf.GetResource(gSProxyRdfRoot)); //Get
  34.             }catch(err){
  35.                 gSProxyRdfC = gSProxyRDFUtil.MakeSeq(gSProxyDs, gSProxyRdf.GetResource(gSProxyRdfRoot)); //Create
  36.             }
  37.         }
  38.                 
  39.     }catch(err){ throw "(switchProxy_ds_initDataSource)\n" + err; }
  40. }
  41.  
  42. //Add RDF Observer
  43. function switchProxy_ds_addObserver(oObserver){
  44.     switchProxy_ds_initDataSource();
  45.     
  46.     try{
  47.         gSProxyDs.AddObserver(oObserver);
  48.     }catch(err){throw "(switchProxy_ds_addObserver)\n" + err}
  49. }
  50.  
  51. //Returns resource for the given uri
  52. function switchProxy_ds_getElement(sUri){
  53.     switchProxy_ds_initDataSource();
  54.     
  55.     try{
  56.         return gSProxyRdf.GetResource(sUri);
  57.     }catch(err){ throw "(switchProxy_ds_getElement)\n" + err; }
  58. }
  59.  
  60. //Duplicate of switchProxy_ds_getElement
  61. function switchProxy_ds_getResource(sAbout){
  62.     return switchProxy_ds_getElement(sAbout)
  63. }
  64.  
  65. //Returns array of all SwitchProxy Element URIs
  66. //    array[index] = uri
  67. function switchProxy_ds_getAllElements(){
  68.     switchProxy_ds_initDataSource();
  69.     
  70.     var aOut        = new Array();
  71.     var aElements    = gSProxyRdfC.GetElements();
  72.     while(aElements.hasMoreElements()){
  73.         var oRes = aElements.getNext();
  74.             oRes = oRes.QueryInterface(Components.interfaces.nsIRDFResource);
  75.         
  76.         aOut[aOut.length] = oRes.Value;
  77.     }
  78.     
  79.     return aOut;
  80. }
  81.  
  82. //Returns an associative array of properties (attributes) contained in sUri element
  83. //    array[propName] = oRes
  84. function switchProxy_ds_getPropertiesFor(sUri){
  85.     switchProxy_ds_initDataSource();
  86.     
  87.     var aOut = new Array();
  88.     
  89.     //Get an array of elements for sAbout
  90.     try{
  91.         var oRes    = switchProxy_ds_getElement(sUri);
  92.         var oTrgts    = gSProxyDs.ArcLabelsOut(oRes);
  93.         while(oTrgts.hasMoreElements()){
  94.             var oTrgt = oTrgts.getNext();
  95.             
  96.             if (oTrgt instanceof Components.interfaces.nsIRDFResource){
  97.                 var sTrgName = oTrgt.Value.substring(gSProxyRdfNodeUriRoot.length + 1); //return node name without URI
  98.                 aOut[sTrgName] = oTrgt;
  99.             }
  100.         }
  101.     }catch(err){throw "(switchProxy_ds_getPropertiesFor)\n" + err}
  102.     
  103.     return aOut;
  104. }
  105.  
  106. //Similar to 'switchProxy_ds_getPropertiesFor' however returns uri=>literal_object_value
  107. //    array[uri] = oLiteral
  108. function switchProxy_ds_getPropertyValuesFor(sUri){
  109.     switchProxy_ds_initDataSource();
  110.     
  111.     var aOut = new Array();
  112.     
  113.     //Get an array of elements for sAbout
  114.     try{
  115.         var oRes    = switchProxy_ds_getElement(sUri);
  116.         var oTrgts    = gSProxyDs.ArcLabelsOut(oRes, true);
  117.         while(oTrgts.hasMoreElements()){
  118.             var oTrgt = oTrgts.getNext();
  119.             
  120.             if (oTrgt instanceof Components.interfaces.nsIRDFResource){
  121.                 aOut[oTrgt.Value] = switchProxy_ds_getValueFor(oRes, oTrgt);
  122.             }
  123.         }
  124.     }catch(err){throw "(switchProxy_ds_getPropertyValuesFor)\n" + err}
  125.     
  126.     return aOut;
  127. }
  128.  
  129. //Returns element that has this property/value
  130. function switchProxy_ds_getElementForValue(sPropertyUri, sValue){
  131.     switchProxy_ds_initDataSource();
  132.     
  133.     try{
  134.         var oValue         = gSProxyRdf.GetLiteral(sValue);
  135.         var oProp         = gSProxyRdf.GetResource(sPropertyUri);
  136.         var oSubject    = gSProxyDs.GetSource(oProp, oValue, true);
  137.         
  138.         return oSubject;
  139.         
  140.     }catch(err){throw "(switchProxy_ds_getElementForValue)\n" + err}
  141.     
  142.     return null;
  143. }
  144.  
  145. // Returns all elements that has this property/value
  146. function switchProxy_ds_getElementsForValue(sPropertyUri, sValue){
  147.     switchProxy_ds_initDataSource();
  148.     
  149.     var aOut = new Array();
  150.     
  151.     try{
  152.         var oValue         = gSProxyRdf.GetLiteral(sValue);
  153.         var oProp         = gSProxyRdf.GetResource(sPropertyUri);
  154.         var aSubject    = gSProxyDs.GetSources(oProp, oValue, true);
  155.         var oSubject    = null
  156.         
  157.         while(aSubject.hasMoreElements()){
  158.             oSubject = aSubject.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  159.             aOut[aOut.length] = oSubject;
  160.         }
  161.         
  162.     }catch(err){throw "(switchProxy_ds_getElementsForValue)\n" + err}
  163.     
  164.     return aOut;
  165. }
  166.  
  167. //Change element's URI
  168. function switchProxy_ds_changeElementUri(oRes, sNewUri){
  169.     switchProxy_ds_initDataSource();
  170.         
  171.     try{
  172.         
  173.         //Get All Properties for element
  174.         var aProps = switchProxy_ds_getPropertyValuesFor(oRes.Value);
  175.         
  176.         //Remove Element
  177.         switchProxy_ds_removeElement(oRes.Value);
  178.         
  179.         //Create element again with new URI
  180.         var newElem = gSProxyRdf.GetResource(sNewUri);
  181.         gSProxyRdfC.AppendElement(newElem);
  182.         for(sProp in aProps){
  183.             gSProxyDs.Assert(newElem, gSProxyRdf.GetResource(sProp), gSProxyRdf.GetLiteral(aProps[sProp]), true);
  184.         }
  185.         
  186.     }catch(err){throw "(switchProxy_ds_changeElementUri)\n" + err}
  187. }
  188.  
  189. //Add element with given sUri, returns added resource
  190. function switchProxy_ds_addElement(sUri){
  191.     switchProxy_ds_initDataSource();
  192.         
  193.     try{
  194.         
  195.         return oRes = gSProxyRdfC.AppendElement(gSProxyRdf.GetResource(sUri));
  196.         
  197.     }catch(err){throw "(switchProxy_ds_addElement)\n" + err}
  198. }
  199.  
  200. //Remove Element for sUri
  201. function switchProxy_ds_removeElement(sUri){
  202.     switchProxy_ds_initDataSource();
  203.     
  204.     try{
  205.         var oRes = gSProxyRdf.GetResource(sUri);
  206.     
  207.         //Remove All Archs
  208.         // Loop for duplicates
  209.         var aArchs        = null;
  210.         var hasArchs    = true;
  211.         while(hasArchs){
  212.             aArchs         = gSProxyDs.ArcLabelsOut(oRes);
  213.             hasArchs     = aArchs.hasMoreElements();
  214.             while(aArchs.hasMoreElements()){
  215.                 oArch = aArchs.getNext().QueryInterface(Components.interfaces.nsIRDFResource);
  216.                 
  217.                 //Remove
  218.                 gSProxyDs.Unassert(oRes, oArch, gSProxyDs.GetTarget(oRes, oArch, true));
  219.             }
  220.         }
  221.         
  222.         //Remove Element
  223.         gSProxyRdfC.RemoveElement(oRes, true);
  224.     }catch(err){throw "(switchProxy_ds_removeElement)\n" + err}
  225. }
  226.  
  227. //Remove property (sUri) from oRes
  228. function switchProxy_ds_removeProperty(sUri, oRes, sValue){
  229.     switchProxy_ds_initDataSource();
  230.     
  231.     try{
  232.         var oPred     = gSProxyRdf.GetResource(sUri);
  233.         var aValues    = switchProxy_ds_getValuesFor(oRes, oPred);
  234.              
  235.     if(typeof(sValue) == 'undefined'){
  236.         // Make sure to delete all properites of this sUri
  237.         for(var i = 0; i < aValues.length; i++){ 
  238.             gSProxyDs.Unassert(oRes, oPred, gSProxyRdf.GetLiteral(aValues[i]) );
  239.         }
  240.     }
  241.     else{
  242.         // Only delete the property with this value
  243.         sValue = gSProxyRdf.GetLiteral(sValue);
  244.         if(gSProxyDs.HasAssertion(oRes, oPred, sValue, true)){
  245.             gSProxyDs.Unassert(oRes, oPred,  sValue);
  246.         }
  247.     }
  248.         
  249.     }catch(err){throw "(switchProxy_ds_removeProperty)\n" + err}
  250. }
  251.  
  252. //Does URI Exist
  253. function switchProxy_ds_doesProxyElementExist(sProxyUri){
  254.     switchProxy_ds_initDataSource();
  255.     
  256.     try{
  257.         var aElems = switchProxy_ds_getAllElements();
  258.         for(var e = 0; e < aElems.length; e++){
  259.             if(aElems[e] == sProxyUri)
  260.                 return true;
  261.         }        
  262.     }catch(err){throw "(switchProxy_ds_doesElementExist)\n" + err}
  263. }
  264.  
  265. //Get Index of URI
  266. function switchProxy_ds_indexOf(sProxyUri){
  267.     switchProxy_ds_initDataSource();
  268.     
  269.     try{
  270.         return gSProxyRdfC.IndexOf(switchProxy_ds_getResource(sProxyUri));
  271.     }catch(err){throw "(switchProxy_ds_indexOf)\n" + err}
  272. }
  273.  
  274. //Rename oProp's URI to sNewUri
  275. function switchProxy_ds_renamePropertyUri(oRes, oProp, sNewUri){
  276.     switchProxy_ds_initDataSource();
  277.     
  278.     try{
  279.         var sValue = gSProxyDs.GetTarget(oRes, oProp, true).QueryInterface(Components.interfaces.nsIRDFLiteral);
  280.         
  281.         gSProxyDs.Unassert(oRes, oProp, gSProxyDs.GetTarget(oRes, oProp, true));
  282.         gSProxyDs.Assert(oRes, gSProxyRdf.GetResource(sNewUri), sValue, true);
  283.         
  284.     }catch(err){throw "(switchProxy_ds_renamePropertyUri)\n" + err}
  285. }
  286.  
  287. //Add Property oProp to oRes
  288. function switchProxy_ds_addProperty(oRes, oProp, sValue, overwriteExisting){
  289.     switchProxy_ds_initDataSource();
  290.         
  291.     try{
  292.         //Don't overwrite it this property exists
  293.         if(!overwriteExisting && gSProxyDs.hasArcOut(oRes, oProp))
  294.             return;
  295.             
  296.         //Add
  297.         gSProxyDs.Assert(oRes, oProp, gSProxyRdf.GetLiteral(sValue), true);
  298.     }catch(err){throw "(switchProxy_ds_addProperty)\n" + err}
  299. }
  300.  
  301. //Change Property value for sPropUri in oRes
  302. function switchProxy_ds_changePropertyValue(oRes, sPropUri, sValue){
  303.     switchProxy_ds_initDataSource();
  304.         
  305.     try{
  306.         var oProp = gSProxyRdf.GetResource(sPropUri);
  307.         
  308.         //Get old value
  309.         var sOld = switchProxy_ds_getValueFor(oRes, oProp);
  310.         
  311.         //Change
  312.         gSProxyDs.Change(oRes, oProp, gSProxyRdf.GetLiteral(sOld), gSProxyRdf.GetLiteral(sValue));
  313.         
  314.     }catch(err){throw "(switchProxy_ds_changePropertyValue)\n" + err}
  315. }
  316.  
  317. //Get Property Value for Property oProp
  318. function switchProxy_ds_getValueFor(oRes, oProp){
  319.     switchProxy_ds_initDataSource();
  320.     
  321.     try{
  322.         oTrgt = gSProxyDs.GetTarget(oRes, oProp, true);
  323.         
  324.         if(oTrgt instanceof Components.interfaces.nsIRDFLiteral){
  325.             return oTrgt.Value;
  326.         }
  327.         
  328.     }catch(err){throw "(switchProxy_ds_getValueFor)\n" + err}
  329.     
  330.     return null;
  331. }
  332.  
  333. //Get All Property Values for Property oProp
  334. //    This is similiar to switchProxy_ds_getValueFor
  335. //    except it returns ALL values for oProp in an array
  336. //        array[index] = sValue
  337. function switchProxy_ds_getValuesFor(oRes, oProp){
  338.     switchProxy_ds_initDataSource();
  339.  
  340.     var aOut = new Array();
  341.     
  342.     try{
  343.         var aTrgts     = gSProxyDs.GetTargets(oRes, oProp, true)
  344.         var oTrgt    = null;
  345.         
  346.         while(aTrgts.hasMoreElements()){
  347.             oTrgt = aTrgts.getNext()
  348.             
  349.             if(oTrgt instanceof Components.interfaces.nsIRDFLiteral){
  350.                 aOut[aOut.length] = oTrgt.Value;
  351.             }
  352.         }
  353.     }catch(err){throw "(switchProxy_ds_getValueFor)\n" + err}
  354.     
  355.     return aOut;
  356. }
  357.  
  358. //Does Property/Value exist in oRes
  359. function switchproxy_ds_doesPropValueExist(oRes, sPropUri, sValue){
  360.     switchProxy_ds_initDataSource();
  361.         
  362.     try{
  363.         var oProp    = gSProxyRdf.GetResource(sPropUri);
  364.         var aValues    = switchProxy_ds_getValuesFor(oRes, oProp);
  365.         
  366.         //Find in array
  367.         for(var i = 0; i < aValues.length; i++){
  368.             if(aValues[i] == sValue)
  369.                 return true;
  370.         }
  371.         
  372.     }catch(err){throw "(switchproxy_ds_doesPropValueExist)\n" + err}
  373.     
  374.     return false;
  375. }
  376.  
  377. /*
  378. * GET PROPERTIES
  379. */
  380.     
  381.     //Get RDF Container
  382.     function switchProxy_ds_getRDFContainer(){
  383.         switchProxy_ds_initDataSource();
  384.         return gSProxyRdf;
  385.     }
  386.     
  387.